<template>
  <div class="login-container">
    <div class="bar">
      <div style="height: 3rem"></div>
      <van-nav-bar :fixed="true" title="登入" left-text="返回" left-arrow @click-left="returnPage" @click-right="editArticleInfo">
        <template #right>
          <van-icon name="add-o" size="18" />
        </template>
      </van-nav-bar>
    </div>
    <!-- 登录表单 -->
    <van-form @submit="onSubmit">
      <van-field v-model="phone" name="用户名" placeholder="请输入手机号" :rules="phoneRules" type="number" maxlength="11" clearable>
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field v-model.lazy="phoneCode" name="验证码" placeholder="请输入验证码" :rules="codeRules" type="number" maxlength="6" clearable>
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <van-button class="send-sms-btn" round size="small" type="default" @click="sendCode">发送验证码</van-button>
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button class="login-btn" block type="info" native-type="submit"> 登录 </van-button>
      </div>
    </van-form>
    <!-- /登录表单 -->
  </div>
</template>
<script>
import { Toast } from 'vant'
import requestApi from '@/utils/request.js'
export default {
  name: 'LoginIndex',
  components: {},
  props: {},
  data() {
    return {
      phone: '',
      phoneCode: ''
    }
  },
  computed: {
    phoneRules() {
      return [
        { required: true, message: '手机号码不能为空' },
        { pattern: /^1\d{10}$/, message: '手机号码格式不正确' }
      ]
    },
    codeRules() {
      return [
        { required: true, message: '验证码不能为空' },
        { pattern: /^\d{6}$/, message: '验证码格式不正确' }
      ]
    }
  },
  watch: {},
  created() {},
  mounted() {},
  methods: {
    returnPage() {
      this.$router.back()
    },
    // 新增文章
    editArticleInfo() {
      this.$router.push('/edit')
    },
    onSubmit() {
      const then = this
      requestApi
        .post('login', {
          phone: then.phone,
          phoneCode: then.phoneCode
        })
        .then(function (response) {
          const req = response.data
          if (req.code === 0) {
            localStorage.setItem('my_token', req.data.token)
            then.$router.push('/user')
          } else {
            Toast.fail(req.msg)
          }
        })
        .catch(function (error) {
          console.log(error)
          Toast.fail('登入错误')
        })
    },
    // 发送验证码
    sendCode() {
      const then = this
      requestApi
        .post('sendCode', {
          phone: then.phone
        })
        .then(function (response) {
          const req = response.data
          if (req.code === 0) {
            console.log(req.data)
          }
        })
        .catch(function (error) {
          console.log(error)
          Toast.fail('发送验证码错误')
        })
    }
  }
}
</script>
<style scoped lang="less">
.login-container {
  .toutiao {
    font-size: 37px;
  }
  .send-sms-btn {
    width: 152px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
}
</style>
